<?php
/**
 * LowCalVanilla Functions tests
 *
 * @package Extensions
 * @subpackage test
 * @copyright Damien Lebrun 2007
 * @author Damien Lebrun
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */

if (!defined('IN_VANILLA')) {
	require('./ini_test.php');

	define('STANDALONE_TEST', 1);
}

/**
 * LowCalVanilla Functions tests
 * @package Extensions
 */
class TestFunctions extends UnitTestCase {

	var $Context;
	var $PeopleScripts = array('js/global.js');
	var $VanillaScripts = array('js/global.js', 'js/vanilla.js', 'js/ajax.js', 'js/ac.js');

	function setUp() {
		$this->Context = new TestContext();
	}

	function CreateFileList()
	{
		$File = new LowCalVanilla_File($this->Context,
				$this->Context->Configuration['LOWCALVANILLA_CONF_DIR']
				. '/FileList.php');
		$File->SetContents('<?php
$LowCalVanilla_FileList[] = \'js/ac.js\' ;
$LowCalVanilla_FileList[] = \'js/ajax.js\' ;
$LowCalVanilla_FileList[] = \'js/builder.js\' ;
$LowCalVanilla_FileList[] = \'js/controls.js\' ;
$LowCalVanilla_FileList[] = \'js/dragdrop.js\' ;
$LowCalVanilla_FileList[] = \'js/effects.js\' ;
$LowCalVanilla_FileList[] = \'js/global.js\' ;
$LowCalVanilla_FileList[] = \'js/jquery.js\' ;
$LowCalVanilla_FileList[] = \'js/prototype.js\' ;
$LowCalVanilla_FileList[] = \'js/slider.js\' ;
$LowCalVanilla_FileList[] = \'js/vanilla.js\' ;
');
	}

	function SetHead($Page, $Scripts) {
		$Head = new Head($this->Context);
		if ($Page == 'vanilla') {
			$Scripts = array_merge($this->VanillaScripts, $Scripts);
		} else if ($Page == 'people') {
			$Scripts = array_merge($this->PeopleScripts, $Scripts);
		}
		foreach ($Scripts as $script) {
			$Head->AddScript($script);
		}
		return $Head;
	}

	function testIsGzipIncompatibleClient() {
		unset($_SERVER['HTTP_USER_AGENT']);
		$this->assertFalse(LowCalVanilla_IsGzipIncompatibleClient(false));
		$this->assertTrue(LowCalVanilla_IsGzipIncompatibleClient(true));

		$Firefox2 = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';
		$IE7 = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; Alexa Toolbar)';
		$Opera9 = 'Opera/9.00 (Windows NT 5.1; U; en)';
		$Safari2 = 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/417.9 (KHTML, like Gecko) Safari/417.8';
		$Safari3 = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; bg) AppleWebKit/522.13.1 (KHTML, like Gecko) Version/3.0.2 Safari/522.13.1';
		$IE6 = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
		$IE55 = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)';


		$_SERVER['HTTP_USER_AGENT'] = $Firefox2;
		$this->assertFalse(LowCalVanilla_IsGzipIncompatibleClient());
		$_SERVER['HTTP_USER_AGENT'] = $IE7;
		$this->assertFalse(LowCalVanilla_IsGzipIncompatibleClient());
		$_SERVER['HTTP_USER_AGENT'] = $Opera9;
		$this->assertFalse(LowCalVanilla_IsGzipIncompatibleClient());
		$_SERVER['HTTP_USER_AGENT'] = $Safari2;
		$this->assertFalse(LowCalVanilla_IsGzipIncompatibleClient());
		$_SERVER['HTTP_USER_AGENT'] = $Safari3;
		$this->assertFalse(LowCalVanilla_IsGzipIncompatibleClient());


		$_SERVER['HTTP_USER_AGENT'] = $IE55;
		$this->assertTrue(LowCalVanilla_IsGzipIncompatibleClient());
		$_SERVER['HTTP_USER_AGENT'] = $IE6;
		$this->assertTrue(LowCalVanilla_IsGzipIncompatibleClient());
	}

	function testAddFileToList() {
		$this->CreateFileList();
		$this->assertTrue(
			LowCalVanilla_AddScriptToList($this->Context, array('js/foo.js'))
		);
		$LowCalVanilla_FileList = array();
		include($this->Context->Configuration['LOWCALVANILLA_CONF_DIR'] . 'FileList.php');
		$this->assertTrue(in_array('js/foo.js', $LowCalVanilla_FileList));
	}

	function testUpdateScriptList() {
		$this->CreateFileList();
		$this->assertTrue(
			LowCalVanilla_UpdateScriptList($this->Context, array('js/foo.js'))
		);
		$LowCalVanilla_FileList = array();
		include($this->Context->Configuration['LOWCALVANILLA_CONF_DIR'] . 'FileList.php');
		$this->assertTrue(in_array('js/foo.js', $LowCalVanilla_FileList));

		$this->assertTrue(
			LowCalVanilla_UpdateScriptList($this->Context, array('js/foo.js', 'js/bar.js'))
		);
		$LowCalVanilla_FileList = array();
		include($this->Context->Configuration['LOWCALVANILLA_CONF_DIR'] . 'FileList.php');
		$this->assertTrue(in_array('js/foo.js', $LowCalVanilla_FileList));
		$this->assertTrue(in_array('js/bar.js', $LowCalVanilla_FileList));

		$this->assertNull(
			LowCalVanilla_UpdateScriptList($this->Context, array('js/foo.js', 'js/bar.js'))
		);
	}

	function testPackJsFiles() {
		$this->CreateFileList();
		$this->Context->Configuration['SELF_URL'] = 'index.php';
		$Head = $this->SetHead('vanilla', array('js/foo.js', 'js/bar.js'));
		LowCalVanilla_PackJsFiles($Head);
		$this->assertEqual(3, count($Head->Scripts));
		$this->assertEqual(
			'/extensions/LowCalVanilla/packer.php?v=test&amp;type=js&amp;f[]=6&amp;f[]=10&amp;f[]=1&amp;f[]=0',
			$Head->Scripts[0]);

		$Head = $this->SetHead('vanilla', array('js/bar.js'));
		$this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS'] = array('js/foo.js');
		LowCalVanilla_PackJsFiles($Head);
		$this->assertEqual(2, count($Head->Scripts));
		$this->assertEqual(
			'/extensions/LowCalVanilla/packer.php?v=test&amp;type=js&amp;f[]=6&amp;f[]=10&amp;f[]=1&amp;f[]=0&amp;f[]=11',
			$Head->Scripts[0]);

		$Head = $this->SetHead('vanilla', array('js/prototype.js', 'js/scriptaculous.js', 'js/bar.js'));
		$this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS'] = array('js/foo.js');
		LowCalVanilla_PackJsFiles($Head);
		$this->assertEqual(2, count($Head->Scripts));
		$this->assertEqual(
			'/extensions/LowCalVanilla/packer.php?v=test&amp;type=js&amp;f[]=6&amp;f[]=10&amp;f[]=1&amp;f[]=0'
				. '&amp;f[]=8'
				. '&amp;f[]=2&amp;f[]=5&amp;f[]=4&amp;f[]=3&amp;f[]=9&amp;f[]=11',
			$Head->Scripts[0]);
	}

	function testAcceptGzip() {
		$Firefox2 = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';
		$IE55 = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)';

		$_SERVER['HTTP_USER_AGENT'] = $Firefox2;
		$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate';
		$this->Context->Configuration['LOWCALVANILLA_GZIP_ALLOWED'] = '1';
		$this->assertTrue(LowCalVanilla_AcceptGzip($this->Context->Configuration));


		$_SERVER['HTTP_USER_AGENT'] = $Firefox2;
		$_SERVER['HTTP_ACCEPT_ENCODING'] = '';
		$this->Context->Configuration['LOWCALVANILLA_GZIP_ALLOWED'] = '1';
		$this->assertFalse(LowCalVanilla_AcceptGzip($this->Context->Configuration));

		$_SERVER['HTTP_USER_AGENT'] = $Firefox2;
		$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate';
		$this->Context->Configuration['LOWCALVANILLA_GZIP_ALLOWED'] = '0';
		$this->assertFalse(LowCalVanilla_AcceptGzip($this->Context->Configuration));

		$_SERVER['HTTP_USER_AGENT'] = $Firefox2;
		$_SERVER['HTTP_ACCEPT_ENCODING'] = '';
		$this->Context->Configuration['LOWCALVANILLA_GZIP_ALLOWED'] = '0';
		$this->assertFalse(LowCalVanilla_AcceptGzip($this->Context->Configuration));


		$_SERVER['HTTP_USER_AGENT'] = $IE55;
		$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate';
		$this->Context->Configuration['LOWCALVANILLA_GZIP_ALLOWED'] = '1';
		$this->assertFalse(LowCalVanilla_AcceptGzip($this->Context->Configuration));

		$_SERVER['HTTP_USER_AGENT'] = $IE55;
		$_SERVER['HTTP_ACCEPT_ENCODING'] = '';
		$this->Context->Configuration['LOWCALVANILLA_GZIP_ALLOWED'] = '1';
		$this->assertFalse(LowCalVanilla_AcceptGzip($this->Context->Configuration));

		$_SERVER['HTTP_USER_AGENT'] = $IE55;
		$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate';
		$this->Context->Configuration['LOWCALVANILLA_GZIP_ALLOWED'] = '0';
		$this->assertFalse(LowCalVanilla_AcceptGzip($this->Context->Configuration));

		$_SERVER['HTTP_USER_AGENT'] = $IE55;
		$_SERVER['HTTP_ACCEPT_ENCODING'] = '';
		$this->Context->Configuration['LOWCALVANILLA_GZIP_ALLOWED'] = '0';
		$this->assertFalse(LowCalVanilla_AcceptGzip($this->Context->Configuration));
	}

	function testGetFilesToMerge() {
		$this->CreateFileList();
		LowCalVanilla_AddScriptToList($this->Context, array('js/foo.js', 'js/bar.js', 'baz.js'));
		$_GET['type'] = 'js';
		$_GET['f'] = array('1', '11', '12', '13', '14');
		$MergedFiles = LowCalVanilla_GetFilesToMerge($this->Context);
		$this->assertTrue($MergedFiles);
		$this->assertEqual(3, count($MergedFiles->_ArrayFiles));
	}
}

if (defined('STANDALONE_TEST')) {

	$test = new TestFunctions();
	$test->run(new HtmlReporter());
}
